﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common.Base;
using Common.Base.Result;
using Common.Enums;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Model.Dto;
using Repository;
using Service;

namespace WebApp.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class IndexController : ControllerBase
    {
        private readonly ILogger<IndexController> _logger;
        private readonly IUserInfoService _infoService;
        private readonly IConfiguration _configurationSection;

        public IndexController(ILogger<IndexController> logger, IUserInfoService infoService,
            IConfiguration configurationSection)
        {
            _logger = logger;
            _infoService = infoService;
            _configurationSection = configurationSection;
        }

        [HttpGet]
        public string Index()
        {
            return "Hello World! " + _configurationSection["Tag"];
        }


        [HttpGet("page")]
        public async Task<ResultData<PageData<UserInfoDto>>> Page()
        {
            
            // var result = await _service.GetQualityUserPage(dto);
            // return PageResult<QualityUserDto>.success(result);
            
            var result = new ResultData<PageData<UserInfoDto>>
            {
                Data = await _infoService.SelectUserInfoPage(new UserInfoPageParamDto())
            };
            return await Task.FromResult(result);
        }

        [HttpGet("users")]
        public async Task<ResultData<List<UserInfoDto>>> UserList()
        {
            var result = new ResultData<List<UserInfoDto>>
            {
                Data = await _infoService.SelectUserInfos(new UserInfoDto())
            };
            return await Task.FromResult(result);
        }

        [HttpGet("user")]
        public async Task<ResultData<UserInfoDto>> User(long id)
        {
            var dto = new UserInfoDto
            {
                KeyId = id
            };
            var result = new ResultData<UserInfoDto>
            {
                Data = await _infoService.SelectUserInfo(dto)
            };
            return await Task.FromResult(result);
        }

        [HttpGet("insert")]
        public async Task<ResultData<bool>> Insert()
        {
            var dto = new UserInfoDto
            {
                UserName = "测试用户 " + DateTime.Now.ToString("yyyyMMddHHmmss"),
                Password = "123456",
                Age = 18,
                Sex = SexEnum.男,
                Money = 1000
            };
            var result = new ResultData<bool>
            {
                Data = await _infoService.InsertUserInfo(dto)
            };
            return await Task.FromResult(result);
        }

        [HttpGet("update")]
        public async Task<ResultData<bool>> Update()
        {
            var dto = new UserInfoDto
            {
                KeyId = 1,
                UserName = "测试用户 " + DateTime.Now.ToString("yyyyMMddHHmmss"),
                Password = "123456",
                Age = 18,
                Sex = SexEnum.男,
                Money = 1000
            };
            var result = new ResultData<bool>
            {
                Data = await _infoService.UpdateUserInfo(dto)
            };
            return await Task.FromResult(result);
        }

        [HttpGet("token")]
        public async Task<ResultData<bool>> Token()
        {
            var result = new ResultData<bool>
            {
                Data = await _infoService.TokenTest()
            };
            return await Task.FromResult(result);
        }

        [HttpGet("custom")]
        public async Task<ResultData<bool>> Custom()
        {
            var result = new ResultData<bool>
            {
                Data = await _infoService.CustomTest()
            };
            return await Task.FromResult(result);
        }
    }
}